<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width:300px;
            height: 200px;
            border:1px solid red;
        }
    </style>
</head>
<body>
    <div id="mydiv"></div>
    <input type="text" id="inputMsg">
    <script>
        var obj={};//定义一个用于完成数据劫持的对象
        var inputMsg=document.getElementById("inputMsg");//获取DOM对象
        var mydiv=document.getElementById("mydiv");//获取DOM对象
        var msgText="呵呵呵";
        Object.defineProperty(obj,"msg",{
            get(){
                console.log("**************触发了get方法*************");
                return msgText;
            },
            set(val){
                console.log("----------触发了set方法----------");
                msgText=val;
                mydiv.innerHTML=val;
            }
        });
        inputMsg.oninput=function(){
            obj.msg=this.value;//会触发set方法
            console.log(obj.msg);//会触发get方法
        };
    </script>
</body>
</html>